/*********************************************************************
*                                                                    *
*                SEGGER Microcontroller GmbH & Co. KG                *
*        Solutions for real time microcontroller applications        *
*                                                                    *
**********************************************************************
*                                                                    *
* C-file generated by:                                               *
*                                                                    *
*        GUI_Builder for emWin version 5.26                          *
*        Compiled Aug  8 2014, 14:49:54                              *
*        (c) 2013 Segger Microcontroller GmbH & Co. KG               *
*                                                                    *
**********************************************************************
*                                                                    *
*        Internet: www.segger.com  Support: support@segger.com       *
*                                                                    *
**********************************************************************
*/

// USER START (Optionally insert additional includes)
// USER END

#include "DIALOG.h"
#include "DLG.h"
#include "stdio.h"
#include "sys.h"
#include "Motor.h"
/*********************************************************************
*
*       Defines
*
**********************************************************************
*/
#define ID_FRAMEWIN_0 (GUI_ID_USER + 0x00)
#define ID_SLIDER_0 (GUI_ID_USER + 0x01)
#define ID_BUTTON_0 (GUI_ID_USER + 0x02)
#define ID_BUTTON_1 (GUI_ID_USER + 0x03)
#define ID_BUTTON_2 (GUI_ID_USER + 0x05)
#define ID_TEXT_0 (GUI_ID_USER + 0x06)
#define ID_IMAGE_0 (GUI_ID_USER + 0x07)
#define ID_SLIDER_1 (GUI_ID_USER + 0x08)
#define ID_IMAGE_1 (GUI_ID_USER + 0x09)
#define ID_TEXT_1 (GUI_ID_USER + 0x0A)
#define ID_IMAGE_2 (GUI_ID_USER + 0x0B)
#define ID_IMAGE_3 (GUI_ID_USER + 0x0C)
#define ID_TEXT_2 (GUI_ID_USER + 0x0D)
#define ID_CHECKBOX_0 (GUI_ID_USER + 0x0E)
#define ID_TEXT_3 (GUI_ID_USER + 0x0F)

extern GUI_BITMAP bmfengshan_icon;
extern GUI_BITMAP bmpaiqishan_icon;
extern GUI_BITMAP bmchuanglian_icon;
extern GUI_BITMAP bmwindos_icon;
extern GUI_FONT GUI_Fontshuju_anniu40;
extern GUI_FONT GUI_Fontkongzhi_font24;
extern GUI_FONT GUI_FontAuto24;


u8 control_flag;
char fanshan_data[20];
char dis_data[20] = "风扇转速:0%";
char dis_data2[20] = "窗帘状态:全关";
int dis_Value = 0;
int WindosValue = 0;
u8 paiqishanFlag = 0;

/*********************************************************************
*
*       Static data
*
**********************************************************************
*/

// USER START (Optionally insert additional static data)
// USER END

/*********************************************************************
*
*       _aDialogCreate
*/
static const GUI_WIDGET_CREATE_INFO _aDialogCreate[] = {
	{ FRAMEWIN_CreateIndirect, "Framewin", ID_FRAMEWIN_0, 0, -5, 800, 480, 0, 0x0, 0 },
  { SLIDER_CreateIndirect, "Slider", ID_SLIDER_0, 148, 101, 250, 62, 0, 0x0, 0 },
  { BUTTON_CreateIndirect, "Button", ID_BUTTON_0, 148, 205, 250, 70, 0, 0x0, 0 },
  { BUTTON_CreateIndirect, "Button", ID_BUTTON_1, 530, 205, 250, 70, 0, 0x0, 0 },
  { BUTTON_CreateIndirect, "Button", ID_BUTTON_2, 536, 337, 248, 123, 0, 0x0, 0 },
  { TEXT_CreateIndirect, "Text", ID_TEXT_0, 200, 65, 150, 25, 0, 0x64, 0 },
  { IMAGE_CreateIndirect, "Image", ID_IMAGE_0, 4, 90, 128, 128, 0, 0, 0 },
  { SLIDER_CreateIndirect, "Slider", ID_SLIDER_1, 532, 100, 250, 60, 0, 0x0, 0 },
  { IMAGE_CreateIndirect, "Image", ID_IMAGE_1, 405, 70, 128, 128, 0, 0, 0 },
  { TEXT_CreateIndirect, "Text", ID_TEXT_1, 550, 65, 200, 25, 0, 0x0, 0 },
  { IMAGE_CreateIndirect, "Image", ID_IMAGE_2, 4, 220, 128, 128, 0, 0, 0 },
  { IMAGE_CreateIndirect, "Image", ID_IMAGE_3, 405, 220, 128, 128, 0, 0, 0 },
	{TEXT_CreateIndirect, "Text", ID_TEXT_2, 0, 0, 795, 35, 0, 0x0, 0 },
	{ CHECKBOX_CreateIndirect, "Checkbox", ID_CHECKBOX_0, 137, 381, 130, 25, 0, 0x0, 0 },
	{ TEXT_CreateIndirect, "Text", ID_TEXT_3, 29, 381, 80, 20, 0, 0x64, 0 },
};

/*********************************************************************
*
*       Static code
*
**********************************************************************
*/

// USER START (Optionally insert additional static code)
// USER END

/*********************************************************************
*
*       _cbDialog
*/
static void _cbDialog(WM_MESSAGE * pMsg) {
  int NCode;
  int Id;
	WM_HWIN hItem;
	hItem = pMsg->hWin;	
	FRAMEWIN_SetTitleVis(hItem,0);
	GUI_UC_SetEncodeUTF8();
	paiqishanFlag = TIM_GetCapture1(TIM8);
  switch (pMsg->MsgId) {
	case WM_INIT_DIALOG:
			/*****************************************************************************************
																					样式设置
		*****************************************************************************************/
		SLIDER_SetNumTicks(WM_GetDialogItem(pMsg->hWin, ID_SLIDER_0),5);
		SLIDER_SetRange(WM_GetDialogItem(pMsg->hWin, ID_SLIDER_0),0,100);
	
		SLIDER_SetNumTicks(WM_GetDialogItem(pMsg->hWin, ID_SLIDER_1),4);
		SLIDER_SetRange(WM_GetDialogItem(pMsg->hWin, ID_SLIDER_1),0,4);
		
		hItem = WM_GetDialogItem(pMsg->hWin, ID_TEXT_2);
    TEXT_SetFont(hItem, &GUI_Fontkongzhi_font24);
    TEXT_SetTextAlign(hItem, GUI_TA_HCENTER | GUI_TA_VCENTER);
		TEXT_SetText(hItem,"电器控制");
		TEXT_SetBkColor(hItem,0x000000);
		TEXT_SetTextColor(hItem,0xffffff);
	
		hItem = WM_GetDialogItem(pMsg->hWin, ID_TEXT_3);
		TEXT_SetFont(hItem, &GUI_FontAuto24);
		TEXT_SetText(hItem, "模式:");
    TEXT_SetTextAlign(hItem, GUI_TA_HCENTER | GUI_TA_VCENTER);
	
		hItem = WM_GetDialogItem(pMsg->hWin, ID_TEXT_0);
		TEXT_SetFont(hItem, &GUI_Fontkongzhi_font24);
    TEXT_SetText(hItem, dis_data);
    TEXT_SetTextAlign(hItem, GUI_TA_HCENTER | GUI_TA_VCENTER);	
		
		hItem = WM_GetDialogItem(pMsg->hWin, ID_TEXT_1);
		TEXT_SetFont(hItem, &GUI_Fontkongzhi_font24);
    TEXT_SetText(hItem, dis_data2);
    TEXT_SetTextAlign(hItem, GUI_TA_HCENTER | GUI_TA_VCENTER);	
	
		hItem = WM_GetDialogItem(pMsg->hWin, ID_IMAGE_0);  
		IMAGE_SetBitmap(hItem,&bmfengshan_icon);
		hItem = WM_GetDialogItem(pMsg->hWin, ID_IMAGE_1);
    IMAGE_SetBitmap(hItem,&bmchuanglian_icon);
		hItem = WM_GetDialogItem(pMsg->hWin, ID_IMAGE_2);
    IMAGE_SetBitmap(hItem,&bmpaiqishan_icon);
		hItem = WM_GetDialogItem(pMsg->hWin, ID_IMAGE_3);
    IMAGE_SetBitmap(hItem,&bmwindos_icon);
	
		hItem = WM_GetDialogItem(pMsg->hWin, ID_BUTTON_2);
    BUTTON_SetFont(hItem, &GUI_Fontshuju_anniu40);
    BUTTON_SetText(hItem, "返回");
		
		hItem = WM_GetDialogItem(pMsg->hWin, ID_BUTTON_0);
    BUTTON_SetFont(hItem, &GUI_Fontkongzhi_font24);
    BUTTON_SetText(hItem, "开");
		
		hItem = WM_GetDialogItem(pMsg->hWin, ID_BUTTON_1);
    BUTTON_SetFont(hItem, &GUI_Fontkongzhi_font24);
		
		hItem = WM_GetDialogItem(pMsg->hWin, ID_CHECKBOX_0);
    CHECKBOX_SetText(hItem, "智能调节");
    CHECKBOX_SetFont(hItem, &GUI_FontAuto24);
		if(control_flag){
				CHECKBOX_SetState(hItem = WM_GetDialogItem(pMsg->hWin, ID_CHECKBOX_0),1);
			}else{
			CHECKBOX_SetState(hItem = WM_GetDialogItem(pMsg->hWin, ID_CHECKBOX_0),0);
			}
		
		
    if(ReadMotorStatus()){
			BUTTON_SetText(WM_GetDialogItem(pMsg->hWin, ID_BUTTON_1), "关");
		}
		else{
			BUTTON_SetText(WM_GetDialogItem(pMsg->hWin, ID_BUTTON_1), "开");
		}
		break;
		/*****************************************************************************************
		*****************************************************************************************/
  case WM_NOTIFY_PARENT:
    Id    = WM_GetId(pMsg->hWinSrc);
    NCode = pMsg->Data.v;
		
		switch(Id) {
     case ID_CHECKBOX_0: // Notifications sent by 'Checkbox'
      switch(NCode) {
      case WM_NOTIFICATION_VALUE_CHANGED:
				control_flag = CHECKBOX_GetState(WM_GetDialogItem(pMsg->hWin, ID_CHECKBOX_0));
        break;
      }
			break;
		case ID_SLIDER_0: // Notifications sent by 'Slider'
      switch(NCode) {
      case WM_NOTIFICATION_CLICKED:
        break;
      case WM_NOTIFICATION_RELEASED:
        break;
      case WM_NOTIFICATION_VALUE_CHANGED:	
				dis_Value = SLIDER_GetValue(WM_GetDialogItem(pMsg->hWin, ID_SLIDER_0));
				TEXT_SetFont(hItem, &GUI_Fontkongzhi_font24);
				sprintf(fanshan_data,"风扇转速:%d%%",dis_Value);
				TEXT_SetText(WM_GetDialogItem(pMsg->hWin, ID_TEXT_0), fanshan_data);
				TIM_SetCompare2(TIM2,dis_Value);
				sprintf(dis_data,"风扇转速:%d%%",dis_Value);
				break;
      }
      break;
		 case ID_SLIDER_1: // Notifications sent by 'Slider'
      switch(NCode) {
      case WM_NOTIFICATION_CLICKED:
        break;
      case WM_NOTIFICATION_RELEASED:
        break;
      case WM_NOTIFICATION_VALUE_CHANGED:
					WindosValue = 25 * SLIDER_GetValue(WM_GetDialogItem(pMsg->hWin, ID_SLIDER_1));
					if(WindosValue >= 0){
						sprintf(dis_data2,"窗帘状态:全关");
						TIM_SetCompare1(TIM1,195); // 0度
					}
					if(WindosValue >= 25){
						sprintf(dis_data2,"窗帘状态:1/4开");
						TIM_SetCompare1(TIM1,190); // 45度
					}
					if(WindosValue >= 50){
							sprintf(dis_data2,"窗帘状态:半开");
						TIM_SetCompare1(TIM1,185); // 90度
					}
					if(WindosValue >= 75){
						sprintf(dis_data2,"窗帘状态:3/4开");
						TIM_SetCompare1(TIM1,180); // 135度
					}
					if(WindosValue == 100){
						sprintf(dis_data2,"窗帘状态:全开");
						TIM_SetCompare1(TIM1,175); // 180度
					}
					TEXT_SetText(WM_GetDialogItem(pMsg->hWin, ID_TEXT_1), dis_data2);
				break;
      }
      break;
    case ID_BUTTON_0: // Notifications sent by 'Button'
      switch(NCode) {
      case WM_NOTIFICATION_CLICKED:
        break;
      case WM_NOTIFICATION_RELEASED:
    		if(ReadMotorStatus()){
						BUTTON_SetText(WM_GetDialogItem(pMsg->hWin, ID_BUTTON_0), "开");
						MotorPWM = 0;
						printf("dianji2:%d\r\n",ReadMotorStatus());		
					}else if(!ReadMotorStatus()){
						BUTTON_SetText(WM_GetDialogItem(pMsg->hWin, ID_BUTTON_0), "关");
						MotorPWM = 1;
						printf("dianji3:%d\r\n",ReadMotorStatus());
					}					
        break;
      }
      break;
    case ID_BUTTON_1: // Notifications sent by 'Button'
      switch(NCode) {
      case WM_NOTIFICATION_CLICKED:
        break;
      case WM_NOTIFICATION_RELEASED:
				if(paiqishanFlag == 175){
				BUTTON_SetText(WM_GetDialogItem(pMsg->hWin, ID_BUTTON_1), "开");
					TIM_SetCompare1(TIM8,195); // 0度
				}
				if(paiqishanFlag == 195){
				BUTTON_SetText(WM_GetDialogItem(pMsg->hWin, ID_BUTTON_1), "关");
				TIM_SetCompare1(TIM8,175); // 180度			
				}
        break;
      }
      break;
    case ID_BUTTON_2: // Notifications sent by 'Button'
      switch(NCode) {
      case WM_NOTIFICATION_CLICKED:
        break;
      case WM_NOTIFICATION_RELEASED:
					GUI_EndDialog(pMsg->hWin,0);
					CreateFramewin();
        break;
      }
      break;
    }
    break;
  default:
    WM_DefaultProc(pMsg);
    break;
  }
}

/*********************************************************************
*
*       Public code
*
**********************************************************************
*/
/*********************************************************************
*
*       CreateFramewin
*/
WM_HWIN ElectricalControl(void);
WM_HWIN ElectricalControl(void) {
  WM_HWIN hWin;
  hWin = GUI_CreateDialogBox(_aDialogCreate, GUI_COUNTOF(_aDialogCreate), _cbDialog, WM_HBKWIN, 0, 0);
  return hWin;
}


/*************************** End of file ****************************/
